<template>
	<view class="address">
		<!-- 边框 -->
		<view class="address_border"></view>
		<!-- 数据 -->
		<view class="address_box" v-for="(item,index) in addressList" :key="index">
			<view class="address_bos info_border" @click="Ondetails(item)">
				<view class="address_info">
					<view class="address_info_box">
						<view class="address_name font-323232 font14">{{item.name}}</view>
						<view class="address_phone font-323232 font14">{{item.phone}}</view>
						<view class="address_mr" v-if="item.isdefault == 1">默认</view>
					</view>
					<view class="address_address font-323232 font14">
						{{item.province_name}}{{item.city_name}}{{item.region_name}}{{item.detail}}
					</view>
				</view>
				<view class="address_edit" @click.stop="Onedit(item)">
					<image src="../../static/my/edit.png"></image>
				</view>
			</view>
			<view class="edit_bos">
				<view class="edit_box" @click.stop="OnsetNormal(item)">
					<image v-if="item.isdefault == 1" src="../../static/my/mor_b.png"></image>
					<image v-else src="../../static/my/mor_a.png"></image>
					<view>设为默认</view>
				</view>
				<view class="edit_delete" @click="Ondelete(item)">删除</view>
			</view>
		</view>
		<view class="loading" v-if="addressList.length <= 0">暂无收货地址</view>
		<!-- 添加地址 -->
		<!-- 提交 -->
		<view class="button_submit flex-row-center font-ffffff font15" @click="Onadd">
			<view class="flex-row-center">添 加 地 址</view>
		</view>
		<view class="button_submitx"></view>
		<!-- <view class="custom_class" @click="Onadd">添 加 地 址</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				addressobj: {}, //默认地址
				isChoose: false,
			}
		},
		onLoad(options) {
			// console.log('我又进来了')
			this.type = options.type
			this.OngetAddress()
			if (options.type === 'confirm') {
				this.isChoose = true
			}
			// 开启监听新增页面传递过来的值
			uni.$on('type', (res) => {
				console.log(res)
				if (res == 1) {
					this.OngetAddress()
				}
			})
		},
		onUnload() {
			// 关闭判断新增页面传递过来的值
			uni.$off('type')
		},
		methods: {
			//列表
			OngetAddress() {
				this.addressobj = {}
				this.$Http({
					url: 'listAddress',
					data: {},
				}).then(res => {
					if (res.code == 1) {
						this.addressList = res.data
						res.data.forEach(item => {
							if (item.isdefault == 1) {
								this.addressobj = item
							}
						})
					}
				});
			},
			//设置默认地址
			OnsetNormal(e) {
				if(e.isdefault==1){
					return
				}
				this.$Http({
					url: 'setDefaultAddress',
					type:'POST',
					data: {
						address_id: e.address_id,
						isdefault:1
					},
				}).then(res => {
					if (res.code == 1) {
						this.OngetAddress()
					}
				});
			},
			//删除地址
			Ondelete(e) {
				let that = this
				uni.showModal({
					title: '操作提示',
					confirmText: '确认',
					confirmColor: '#ff4646',
					cancelText: '取消',
					content: '您确认要删除此地址吗',
					success: function(res) {
						if (res.confirm) {
							that.$Http({
								url: 'delAddress',
								data: {
									id: e.address_id
								},
							}).then(res => {
								if (res.code == 1) {
									that.OngetAddress()
								}
							});
						}
					}
				});
			},
			//添加地址
			Onadd() {
				uni.navigateTo({
					url: '/pagesA/my/addressis',
				})
			},
			//编辑地址
			Onedit(e) {
				uni.navigateTo({
					url: '/pagesA/my/addressis?id=' + e.address_id,
				})
			},
			// 选择收货地址
			Ondetails(item) {
				if (this.isChoose) {
					uni.$emit('choosePath', item)
					// 关闭当前页面
					uni.navigateBack({
						delta: 1
					});
				}
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
